package com.qisu.command;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qisu.cacheMap.ChargeQuantity;
import com.qisu.cacheMap.OrderIdCache;
import com.qisu.cacheMap.TimeAndPriceCache;
import com.qisu.converters.Converters;
import com.qisu.model.vo.Gun;
import com.qisu.service.OrderService;

import io.netty.channel.ChannelHandlerContext;

/**
 * 充电指令
 * @author li
 *
 */
@Component
public class CMD65535 extends AbstractSinexcelCmd {
	@Autowired private OrderService orderService;
	
	@Autowired private OrderIdCache orderIdCache;
	@Autowired TimeAndPriceCache timeAndPriceCache;
	@Autowired private ChargeQuantity chargeQuantity;

	@PostConstruct
	@Override
	protected void init() {
		super.init();

		fieldsList.addField(new CommandFieldDef<Integer>(2))
				.addField(new CommandFieldDef<Integer>(2))
				.addField(new CommandFieldDef<String>(40, Converters.strConverter, "单号"));
	}

	@Override
	protected void processCmd(ChannelHandlerContext ctx, byte[] req) {
		System.out.println("65526process");
		String orderId = fieldsList.getValue("单号");
		Gun gun = orderService.getOrderPileAndGun(orderId);
		log.info(gun.toString());
		String pileSerial = gun.getPileSerial();
		orderIdCache.put(gun.getKey(), orderId);
		
//		=>如果结算没有成功移除,此处移除,在信息包里,当重启等还会再次放入。再次查时间与电价。
		chargeQuantity.remove(gun.getKey());
		timeAndPriceCache.remove(gun.getKey());
		/*HashMap<String, Object> packetData = new HashMap<String, Object>() {
			private static final long serialVersionUID = -2251162143864071002L;

			{
				put("单号", _fieldsList.getValue("单号"));
			}
		};*/

//		_publisher.publish("notify/received/start-charge", pileSerial);
//		_publisher.publish("data/received/start-charge/" + pileSerial, _json.writeValueAsString(packetData));

		//_order.setOrderTransactionId(orderId);

		byte[] p = new byte[] {
			(byte)0xAA, (byte)0xF5,	
			(byte)0x3E, (byte)0x00,
			(byte)0x10,
			(byte)0x00,
			(byte)0x07, (byte)0x00,

			(byte)0x00, (byte)0x00,	
			(byte)0x00, (byte)0x00,

			(byte)gun.getGunNo(),

			(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
			(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
			(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
			(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,

			(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
			(byte)0x00,

			(byte)'I', (byte)'D', (byte)'N', (byte)'6', (byte)'0', (byte)'0', (byte)'0', (byte)'0',
			(byte)'0', (byte)'0', (byte)'2', (byte)'0', (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
			(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
			(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,

			(byte)0x00,
			(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,

			(byte)0x00
		};


		byte[] b = pileSerial.getBytes();

		for(int i=0, j=b.length; i<j; i++) {
			p[i+38] = b[i];
		}

		ChannelHandlerContext pileCtx = channelList.get(pileSerial);
		send(pileCtx, p);
		responseClient(ctx);
	}

	@Override
	protected void responseClient(ChannelHandlerContext ctx) {
		log.debug(">>>>>>>>> CTX: " + ctx.channel());
		log.info("返回给APP 200");
		ctx.writeAndFlush("200\n".getBytes());
	}

}
